<#assign j="${r'#'}"/> <#assign h="${r'$'}"/>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${clazz.name?cap_first}Mapper">

    <resultMap type="${packageName}.bean.${clazz.name?cap_first}" id="BaseResult">
    <#list clazz.fields as field>
        <#if field.primary>
        <id property="${field.name}" column="${field.fieldName}"/>
            <#else >
        <result property="${field.name}" column="${field.fieldName}"/>
        </#if>
    </#list>
    </resultMap>

    <sql id="BaseSelect">
        <#assign index=0 />
        select <#list clazz.fields as field><#if (index>0)>,</#if>${field.fieldName}<#assign index=index+1/></#list>
        from ${clazz.clazzName}
    </sql>

    <select id="selectByObj"  parameterType="${packageName}.bean.${clazz.name?cap_first}" resultMap="BaseResult" >
        <include refid="BaseSelect" />
        <where>
        <#list clazz.fields as field>
            <if test="${field.name} != null and ${field.name}.toString() != '' ">
                and  ${field.fieldName}=${j}{${field.name}}
            </if>
        </#list>
        <#list clazz.fields as field>
        <#if field.type=="Date">
            <if test="${field.name}BeginValue != null and ${field.name}BeginValue.toString() != ''">
                and ${field.fieldName} &gt;= ${j}{${field.name}BeginValue}
            </if>
            <if test="${field.name}EndValue != null and ${field.name}EndValue.toString() != ''">
                and ${field.fieldName} &lt;= ${j}{${field.name}EndValue}
            </if>
        </#if>
        </#list>
            <if test="keyword != null and keyword.toString() != '' ">
                and id like concat('%',${j}{keyword},'%')
            </if>
            <if test="dataScope != null and dataScope.toString() != ''">
                and ${h}{dataScope}
            </if>
        </where>
            <if test="groupValue != null and groupValue.toString() != ''">
                group by ${h}{groupValue}
            </if>
            <if test="orderValue != null and orderValue.toString() != ''">
                order by ${h}{orderValue}
            </if>
    </select>

    <update id="updateBatchByIds">
        update ${clazz.clazzName}
        <set>
        <#list clazz.fields as field>
            <if test="obj.${field.name} != null and obj.${field.name}.toString() != '' ">
                ,${field.fieldName}=${j}{obj.${field.name}}
            </if>
        </#list>
        </set>
        where id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            ${j}{id}
        </foreach>
    </update>

    <select id="selectCountByObj"  parameterType="${packageName}.bean.${clazz.name?cap_first}" resultType="java.lang.Long" >
        select count(*)
        from ${clazz.clazzName}
        <where>
        <#list clazz.fields as field>
            <if test="${field.name} != null and ${field.name}.toString() != '' ">
                and  ${field.fieldName}=${j}{${field.name}}
            </if>
        </#list>
        <#list clazz.fields as field>
            <#if field.type=="Date">
            <if test="${field.name}BeginValue != null and ${field.name}BeginValue.toString() != ''">
                and ${field.fieldName} &gt;= ${j}{${field.name}BeginValue}
            </if>
            <if test="${field.name}EndValue != null and ${field.name}EndValue.toString() != ''">
                and ${field.fieldName} &lt;= ${j}{${field.name}EndValue}
            </if>
          </#if>
        </#list>
            <if test="dataScope != null and dataScope.toString() != ''">
                and ${h}{dataScope}
            </if>
        </where>
    </select>

</mapper>